package com.thunder.kocho.transition

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import com.thunder.kocho.loki.TripleCircle

class DestinyView: androidx.appcompat.widget.AppCompatImageView {

    private lateinit var yellowDrawable:TripleCircle
    private lateinit var redDrawable: GradientDrawable
    private lateinit var greenDrawable:GradientDrawable

    private var mState:Int=0
    private val yellowState=0
    private val greenState=1
    private val redState=2

    constructor(context: Context):this(context, null)

    constructor(context: Context, attrs: AttributeSet?):this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int):super(context, attrs, defStyle){
        init()
    }


    private fun init(){
        redDrawable= GradientDrawable()
        redDrawable.setColor(Color.RED)
        greenDrawable= GradientDrawable()
        greenDrawable.setColor(Color.GREEN)
        yellowDrawable=TripleCircle()
        yellowDrawable.color=Color.YELLOW
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var height = 100
        var width = 100
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = heightSize.coerceAtMost(height)
        }
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = width.coerceAtMost(widthSize)
        }
        setMeasuredDimension(width, height)
        yellowDrawable.setBounds(0,0,width,height)
        background=yellowDrawable
        if(visibility== VISIBLE){
            yellowDrawable.start()
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
    }

    override fun onVisibilityChanged(changedView: View, visibility: Int) {
        super.onVisibilityChanged(changedView, visibility)
        when(visibility){
            VISIBLE->{
                if(!yellowDrawable.isStart){
                    yellowDrawable.start()
                }
            }

            INVISIBLE, GONE->{
                if(yellowDrawable.isStart){
                    yellowDrawable.stop()
                }
            }

        }
    }
}